#include <iostream>
using namespace std;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
    int count(TreeNode* root)
    {
        if (nullptr == root)
            return 0;
        return 1 + count(root->left) + count(root->right);
    }
    void record_data(TreeNode* root, int*& array, int& i)
    {
        if (nullptr == root)
            return ;
        array[i++] = root->val;
        record_data(root->left, array, i);
        record_data(root->right, array, i);
    }
public:
    int numColor(TreeNode* root) {
        int size = count(root);
        int* array = new int[size]{ 0 };
        int i = 0;
        record_data(root, array, i);
        sort(array, array + size);
        int count = 1;
        for(int i = 1; i < size; ++i)
        {
            if (array[i] != array[i-1])
                ++count;
        }
        delete[] array;

        return count;
    }
};
